﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;

namespace pigs
{
    class HealthBar
    {
        public Color barColor;
        public Texture2D barTexture;
        public Vector2 position;
        public int screenWidth;
        public float oldHealth;

        public HealthBar(Color c, float health, GraphicsDevice device, Vector2 pos, int width)
        {
            this.oldHealth = (int)(health);
            this.barColor = c;
            this.screenWidth = width;
            this.position = pos;

            makeBar(device);
        }

        /// <summary>
        /// update the health bar
        /// </summary>
        /// <param name="d">graphics device to create the texture</param>
        /// <param name="newH">new health value to use</param>
        public void update(GraphicsDevice d, float newHealth)
        {
            int newH = (int)newHealth;
            if (newH < oldHealth)
            {
                oldHealth--;
            }
            if (newH > oldHealth)
            {
                oldHealth++;
            }

            if (oldHealth > 0)
            {
                makeBar(d);
            }
        }


        public void makeBar(GraphicsDevice d)
        {
            barTexture = new Texture2D(d, (int)oldHealth * 3, 15);

            Color[] data = new Color[(int)oldHealth * 3 * 15];
            for (int i = 0; i < data.Length; ++i) data[i] = Color.Green;
            //top left corner
            data[0].A = 0;
            data[1].A = 0;
            data[barTexture.Width].A = 0;

            //top right corner
            data[barTexture.Width - 2].A = 0;
            data[barTexture.Width - 1].A = 0;
            data[barTexture.Width * 2 - 1].A = 0;

            //bottom right corner
            data[data.Length - 1].A = 0;
            data[data.Length - 2].A = 0;
            data[data.Length - 1 - barTexture.Width].A = 0;

            //bottom left corner
            data[data.Length - barTexture.Width + 1].A = 0;
            data[data.Length - barTexture.Width].A = 0;
            data[data.Length - barTexture.Width * 2].A = 0;

            barTexture.SetData(data);

        }
        /// <summary>
        /// set the position of the health bar
        /// </summary>
        /// <param name="pos">position relative to the top right of the screen</param>
        public void setPosition(Vector2 pos)
        {
            this.position = pos;
        }

        /// <summary>
        /// draw the bar
        /// </summary>
        /// <param name="spritebatch">spritebatch to draw it in</param>
        public void Draw(SpriteBatch spritebatch)
        {
            spritebatch.Draw(barTexture, new Vector2(screenWidth - position.X - barTexture.Width, position.Y), Color.White);
        }
    }
}
